import React from 'react'
import { LockOutlined, UserOutlined } from '@ant-design/icons'
import { Button, Form, Input, Card, message } from 'antd'
import type { FormProps } from 'antd'
import classNames from 'classnames'
import './index.scss'
import { useDispatch } from 'react-redux'
import { fetchLogin } from '@/store/modules/user'
import { useNavigate } from 'react-router-dom'
import { LoginInter } from '@/typings/interface'


const Login: React.FC = () => {
  const dispatch = useDispatch()
  const navigate = useNavigate()

  const onFinish: FormProps<LoginInter>['onFinish'] = async (values) => {
    console.log(values)
    // 触发异步action  fetchLogin
    await dispatch<any>(fetchLogin(values))
    // 1. 跳转到首页
    navigate('/')
    // 2. 提示一下用户
    message.success('登录成功')
  }

  return (
    <div className={classNames('card-content')}>
      <Card style={{ width: 500 }}>
        <div className={classNames('login-title')}>登录</div>
        <Form
          onFinish={onFinish}>
          <Form.Item<LoginInter>
            name="mobile"
            validateTrigger="onBlur"
            rules={[
              { required: true, message: '请输入手机号' },
              { pattern: /1[0-9]\d{9}/, message: '请输入正确的手机号' }
            ]}
          >
            <Input prefix={<UserOutlined />} placeholder='手机号' />
          </Form.Item>
          <Form.Item<LoginInter>
            name="code"
            validateTrigger="onBlur"
            rules={[
              { required: true, message: '请输入验证码' },
            ]}
          >
            <Input prefix={<LockOutlined />} placeholder='验证码' />
          </Form.Item>
          <Form.Item>
            <Button type="primary" block htmlType="submit">登录</Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}
export default Login